﻿using System;
using System.Web;
using Medianamik.Core.Index;
using Medianamik.Core.Sugar;

namespace Medianamik.UI
{
	public abstract class BaseSearchResultPage : BasePage
	{
		protected string Query
		{
			get
			{
				return HttpUtility.UrlDecode(Request["q"]) ?? string.Empty;
			}
		}

		protected Guid? ParentNodeId
		{
			get
			{
				Guid id;
			    return StringHelper.IsGuid(Request["parentId"], out id)
			               ? id
			               : BaseParentNodeId;
			}
		}

		protected abstract Guid BaseNodeTypeId {get;}

        protected abstract Guid BaseParentNodeId { get; }

		protected virtual int PageSize
		{
			get
			{
				return 20;
			}
		}

		private SearchResult _result;
		protected virtual SearchResult Result
		{
			get
			{
				if (_result == null)
				{
					_result = Search();
				}
				return _result;
			}
	}

		private SearchResult Search()
		{
			return Searcher.Search(Query, BaseNodeTypeId, CurrentEditionCulture);
		}
	}
}
